package cn.dakaqi.services.impl;

import cn.dakaqi.dao.ActivityAddressDao;
import cn.dakaqi.entities.ActivityAddress;
import cn.dakaqi.services.ActivityAddressService;
import cn.dakaqi.utils.DKQConstant;
import cn.dakaqi.utils.exception.ServiceRuntimeException;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.persistence.DynamicSpecifications;
import org.springside.modules.persistence.SearchFilter;
import org.springside.modules.utils.Clock;

import java.util.Map;

/**
 * @author chunyang on 2016/4/13.
 */
@Component(value = "activityAddressService")
@Transactional
public class ActivityAddressServiceImpl implements ActivityAddressService
{
    @Autowired
    ActivityAddressDao activityAddressDao;

    @Override
    public void save(String jsonData) throws ServiceRuntimeException
    {
        JSONObject jsonObject = JSON.parseObject(jsonData);
        String memberCode = jsonObject.getString("memberCode");
        String address = jsonObject.getString("address");
        String name = jsonObject.getString("name");
        String province = jsonObject.getString("province");
        String city = jsonObject.getString("city");
        String district = jsonObject.getString("district");
        String lng = jsonObject.getString("lng");
        String lat = jsonObject.getString("lat");
        ActivityAddress activityAddress = new ActivityAddress();
        activityAddress.setAddress(address);
        activityAddress.setCreateTime(Clock.DEFAULT.getCurrentDate());
        activityAddress.setName(name);
        activityAddress.setMemberCode(memberCode);
        activityAddress.setProvince(province);
        activityAddress.setCity(city);
        activityAddress.setDistrict(district);
        if(StringUtils.isNotBlank(lng) && StringUtils.isNotBlank(lat))
        {
            activityAddress.setLng(Double.valueOf(lng));
            activityAddress.setLat(Double.valueOf(lat));
        }
        save(activityAddress);
    }

    @Override
    public void save(ActivityAddress activityAddress) throws ServiceRuntimeException
    {
        this.activityAddressDao.save(activityAddress);
    }

    @Override
    public void update(ActivityAddress activityAddress) throws ServiceRuntimeException
    {
        this.activityAddressDao.save(activityAddress);
    }

    @Override
    public Page<ActivityAddress> findByMemberCode(String memberCode, String name,String clientId, int pageNumber) throws Exception
    {
        Sort sort = new Sort(Sort.Direction.DESC, "createTime");
        PageRequest pageRequest = new PageRequest(pageNumber - 1, 50, sort);

        Map<String, SearchFilter> filters = Maps.newHashMap();
        filters.put("memberCode", new SearchFilter("memberCode", SearchFilter.Operator.EQ, memberCode));
        filters.put("delStatus", new SearchFilter("delStatus", SearchFilter.Operator.EQ, DKQConstant.DEL_NO));
        if(StringUtils.isNotBlank(name))
        {
            filters.put("name", new SearchFilter("name", SearchFilter.Operator.LIKE, name));
        }
        Specification<ActivityAddress> spec = DynamicSpecifications.bySearchFilter(filters.values(), ActivityAddress.class);
        return this.activityAddressDao.findAll(spec, pageRequest);
    }

    @Override
    public ActivityAddress findOne(Long id) throws ServiceRuntimeException
    {
        return this.activityAddressDao.findOne(id);
    }

    @Override
    public void del(Long id) throws ServiceRuntimeException
    {
        ActivityAddress activityAddress = this.findOne(id);
        if(null != activityAddress)
        {
            activityAddress.setDelStatus(DKQConstant.DEL_YES);
            this.save(activityAddress);
        }
    }

    @Override
    public void del(String jsonData) throws ServiceRuntimeException
    {
        JSONObject jsonObject = JSON.parseObject(jsonData);
        String memberCode = jsonObject.getString("memberCode");
        Long activityAddressId = jsonObject.getLong("activityAddressId");
        this.del(activityAddressId);
    }
}
